package org.dromara.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.domain.SysFloor;
import org.dromara.system.domain.bo.SysFloorBo;
import org.dromara.system.domain.vo.SysFloorVo;
import org.dromara.system.mapper.SysFloorMapper;
import org.dromara.system.service.ISysFloorService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 楼栋管理Service业务层处理
 *
 * @author LSL
 * @date 2024-05-23
 */
@RequiredArgsConstructor
@Service
public class SysFloorServiceImpl implements ISysFloorService {

    private final SysFloorMapper baseMapper;

    /**
     * 查询楼栋管理
     */
    @Override
    public SysFloorVo queryById(Long floorId) {
        return baseMapper.selectVoById(floorId);
    }

    /**
     * 查询楼栋管理列表
     */
    @Override
    public TableDataInfo<SysFloorVo> queryPageList(SysFloorBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysFloor> lqw = buildQueryWrapper(bo);
        Page<SysFloorVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询楼栋管理列表
     */
    @Override
    public List<SysFloorVo> queryList(SysFloorBo bo) {
        LambdaQueryWrapper<SysFloor> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysFloor> buildQueryWrapper(SysFloorBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysFloor> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getFloorNum()), SysFloor::getFloorNum, bo.getFloorNum());
        lqw.like(StringUtils.isNotBlank(bo.getName()), SysFloor::getName, bo.getName());
        lqw.eq(bo.getCommunityId() != null, SysFloor::getCommunityId, bo.getCommunityId());
        lqw.eq(bo.getFloorArea() != null, SysFloor::getFloorArea, bo.getFloorArea());
        lqw.eq(bo.getSeq() != null, SysFloor::getSeq, bo.getSeq());
        lqw.eq(StringUtils.isNotBlank(bo.getLat()), SysFloor::getLat, bo.getLat());
        lqw.eq(StringUtils.isNotBlank(bo.getLng()), SysFloor::getLng, bo.getLng());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), SysFloor::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增楼栋管理
     */
    @Override
    public Boolean insertByBo(SysFloorBo bo) {
        SysFloor add = MapstructUtils.convert(bo, SysFloor.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setFloorId(add.getFloorId());
        }
        return flag;
    }

    /**
     * 修改楼栋管理
     */
    @Override
    public Boolean updateByBo(SysFloorBo bo) {
        SysFloor update = MapstructUtils.convert(bo, SysFloor.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysFloor entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除楼栋管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
